package com.qingcheng.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.pojo.goods.Goods;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.service.goods.SpuService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.util.List;

/**
 * 消息监听类,删除sku对应的页面
 */
public class MessageConsumer2 implements MessageListener {
    @Reference
    private SpuService spuService;
    @Value("${pagePath}")
    private String pagePath;
    @Override
    public void onMessage(Message message) {
        String spuid = new String(message.getBody());
        Goods goods = spuService.findGoodsById(spuid);
        List<Sku> skuList = goods.getSkuList();
        goods.getSpu().setIsDelete("1");
        for (Sku sku : skuList) {
            sku.setStatus("0");
            File dest = new File(pagePath + sku.getId() + ".html");
            if(dest.exists()){
                dest.delete();
            }
        }
    }
}
